<?php
	//Pagination Partie 1
	$nombre_classes_page = 20;
	$req = $bdd->query('SELECT COUNT(id) AS nombre_articles_total FROM admin_classes');
	$donnees = $req->fetch();
	$nombre_articles_total=$donnees['nombre_articles_total'];

	$nombre_pages=ceil($nombre_articles_total/$nombre_classes_page);
	if ((isset($_GET['numero_page'])) && ($_GET['numero_page']>0) && ($_GET['numero_page']<=$nombre_pages)){
		$page_courante = $_GET['numero_page'];
	}
	else{
		$page_courante = 1;
	}
	$article_debut=(($page_courante-1)*$nombre_classes_page);
	
	if ((isset($_SESSION['identifiant'])) AND (isset($_SESSION['groupe'])==2))
	{
		?>
		<ul id="pagination">
			<?php
				$suiv = $page_courante + 1; 
				$prec = $page_courante - 1;  
				if ($nombre_pages > 1 && $page_courante > 1 ) {
					if($page_courante == 1 ) {
						?>
						<li class="previous-off"> <<< </li>
						<?php 
					} 
					else  {
						?>
						<li class="previous"><a href="page.php?page=admin_classes&numero_page=<?php echo $prec; ?>"> <<< </a></li>
						<?php    
					}
				}
				for($i=1; $i<=$nombre_pages; $i++) 
				{ 
					if($i==$page_courante) { 
						?>
						<li class="active"><?php echo $i; ?></li>
						<?php
					} 
					else { 
						?>
						<li><a href="page.php?page=admin_classes&numero_page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
						<?php
					} 
				} 
				if ($nombre_pages > 1 && $page_courante < $nombre_pages  ) {
					if($page_courante == $nombre_pages - 1 ) {
						?>
						<li class="next"><a href="page.php?page=admin_classes&numero_page=<?php echo $suiv; ?>"> >>> </li>
						<?php 
					} 
					else {
						?>
						<li class="next-off"> >>> </li>
						<?php
					}
				}
			?>
		</ul>
		<?php
		if (isset($_POST['editer'])) 
		{
			$req = $bdd->prepare('SELECT * FROM admin_classes WHERE id = :id');
			$req->execute(array('id' => $_POST['id']));
			$donnees = $req->fetch();
			
			?>
			<h1>Editer cette classe</h1>
			<form method="post" action="page.php?page=admin_classes">
			<table>
				<tr>
				<td><label for="nom">Nom</label><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td>
				<td><input type="text" name="nom" value="<?php echo $donnees['nom']; ?>" maxlength="250" size="50"></td>
				<td><input type="submit" value="Modifier" name="modifier" /></td></tr>
				<tr><td colspan="3"><textarea id="description" name="description" rows="6" cols="70"><?php echo $donnees['description']; ?></textarea></td></tr>
				</table>
			</form>
			<?php
		}
		elseif (isset($_POST['modifier'])) 
		{
			$req = $bdd->prepare('UPDATE admin_classes SET nom = :nom, description = :description WHERE id=:id');
			$req->execute(array(
				'nom' => $_POST['nom'],
				'description' => $_POST['description'],
				'id' => $_POST['id']
			));
			header('Location: page.php?page=admin_classes'); 
		}
		elseif (isset($_POST['effacer'])) 
		{
			$req = $bdd->prepare('DELETE FROM admin_classes WHERE id=:id');
			$req->execute(array('id' => $_POST['id']));
			header('Location: page.php?page=admin_classes'); 
		}
		elseif (isset($_POST['ajouter'])) 
		{
			$req = $bdd->prepare('INSERT INTO admin_classes (nom) VALUES (:nom)');
			$req->execute(array(
				'nom' => $_POST['nom']
			));
			header('Location: page.php?page=admin_classes');
		}
		else 
		{
			$req = $bdd->query('SELECT * FROM admin_classes ORDER BY id LIMIT '. $article_debut.', '.$nombre_classes_page.'');
			?>
			
			<?php
			if ($nombre_articles_total > 0) 
			{
				?>
				<table width="90%" border="0">
				<h1 id="titre">Gestion des classe</h1>
				<?php
				while ($donnees = $req->fetch())
				{
					?>
					<form method="post" action="page.php?page=admin_classes">
					<tr>
						<td><?php echo $donnees['nom']; ?><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td>	
						<td><input type="submit" value="<?php echo EDITER; ?>" name="editer" /><input type="submit" value="<?php echo EFFACER; ?>" name="effacer" /></td>
					</tr>
					</form>
					<?php 
				}
				?>
				</table>
				<?php
			}
		}
		$req->closeCursor();
		?>

		<h1>Ajouter une nouvelle classe</h1>
		<form method="post" action="page.php?page=admin_classes">
		<table>
			<tr>
			<td><label for="nom">Nom</label></td>
			<td><input type="text" name="nom" maxlength="250" size="45"></td>
			<td><input type="submit" value="Ajouter" name="ajouter" /></td>
			</tr>
		</table>
		</form>
		<?php
	}
	else
	{
		echo NON_ACCES_ADMIN;
	}
?>